﻿using System;
using DAL.Repositories;
using GameAdmin.Classes;

namespace GameAdmin.Pages
{
    public partial class userNew : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            btnSave.Click += new EventHandler(btnSave_Click);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
             
            }
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    DAL.POCO.User u = new DAL.POCO.User();
                    u.login = txtLogin.Text;
                    u.jmeno = txtName.Text;
                    u.prijmeni = txtLastname.Text;
                    u.email = txtEmail.Text;
                    u.admin = chkBoxAdmin.Checked;
                    u.hashedPassword = Security.CreateHash(txtPassword.Text);

                    UserRepository.Insert(u);

                    Response.Redirect("~/users", true);
                }
                catch (Exception ex)
                {
                    lblMessage.Text = ex.Message;
                    lblMessage.Visible = true;
                }
            }
        }
    }
}